﻿Public Class 维修录入

    Dim DHH As String           '单号头
    Dim DH As String            '单号
    Dim YCFY As Integer         '预测费用



#Region "窗体事件"

    '关闭窗体
    Private Sub 维修录入_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        StarRpForm = Nothing
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    '加载窗体
    Private Sub 维修录入_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '生成单号
        DH_calc()

        '加载品牌
        PP_Load()

        '加载维修项
        EV_Load()

        '加载维修员
        WP_Load()

        TextBox1.Focus()

    End Sub
    '生成单号
    Private Sub DH_calc()

        '开始生成单号

        '单号头部
        Dim mon As String = Now.Month.ToString
        If Now.Month < 10 Then
            mon = "0" + Now.Month.ToString
        End If

        Dim daa As String = Now.Day.ToString
        If Now.Day < 10 Then
            daa = "0" + Now.Day.ToString
        End If

        DHH = "W" + Now.Year.ToString + mon + daa
        Debug.Print("")
        Debug.Print("DHH:" + DHH)



        '计算单号

        Dim SHW As String = ""

        Dim sql As String = "select 维修单号 from 维修表  where 维修单号 like '" + DHH + "%'"
        Debug.Print(sql)
        Dim ds As DataSet = SQLtoDataSet(sql)
        If ds.Tables(0).Rows.Count > 0 Then   '当天单存在

            Dim ad As Integer = ds.Tables(0).Rows.Count + 1
            Debug.Print(ad)
            If ad < 10 Then
                SHW = "00" + ad.ToString
            ElseIf ad < 100 Then
                SHW = "0" + ad.ToString
            ElseIf ad < 1000 Then
                SHW = ad.ToString
            Else
                MsgBox("本系统不支持日单量超过999！")
                Exit Sub
            End If
        Else
            Debug.Print("不存在")
            SHW = "001"
        End If

        '完成单号生成
        DH = DHH + SHW
        Label9.Text = DH

    End Sub
    '加载品牌
    Private Sub PP_Load()
        Dim sql As String = "select 品牌 from 品牌表"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim RC As Integer = ds.Tables(0).Rows.Count
        If RC > 0 Then
            ComboBox1.Items.Clear()
            For i = 0 To RC - 1
                ComboBox1.Items.Add(ds.Tables(0).Rows(i).Item(0))
            Next
        End If

    End Sub
    '加载维修项
    Private Sub EV_Load()
        Dim sql As String = "select 项目 from 维修项"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim RC As Integer = ds.Tables(0).Rows.Count
        If RC > 0 Then
            ComboBox3.Items.Clear()
            For i = 0 To RC - 1
                ComboBox3.Items.Add(ds.Tables(0).Rows(i).Item(0))
            Next
        End If
    End Sub
    '加载维修员
    Private Sub WP_Load()
        Dim sql As String = "select 维修员 from 维修员"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim RC As Integer = ds.Tables(0).Rows.Count
        If RC > 0 Then
            ComboBox4.Items.Clear()
            For i = 0 To RC - 1
                ComboBox4.Items.Add(ds.Tables(0).Rows(i).Item(0))
            Next
        End If

    End Sub

#End Region


    '只输入数字
    Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
        NumOnly(e)
    End Sub
    '只输入数字
    Private Sub NumOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub

    '生成任务按扭
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        '输入检测
        Dim a1 As Boolean = (Trim(TextBox1.Text) = "")
        Dim a6 As Boolean = (Trim(TextBox2.Text) = "")

        Dim a2 As Boolean = (Trim(ComboBox1.Text) = "")
        Dim a3 As Boolean = (Trim(ComboBox2.Text) = "")
        Dim a4 As Boolean = (Trim(ComboBox4.Text) = "")
        Dim a5 As Boolean = (Trim(ComboBox3.Text) = "")

        If a1 Then MsgBox("客户联系电话尚未填写！，请填写！") : TextBox1.Focus() : Exit Sub
        If a6 Then MsgBox("客户联系地址尚未填写！，请填写！") : TextBox2.Focus() : Exit Sub

        If a2 Then MsgBox("维修品牌尚未选择！，请选择！") : ComboBox1.Focus() : Exit Sub
        If a3 Then MsgBox("维修型号尚未选择！，请选择！") : ComboBox2.Focus() : Exit Sub
        If a4 Then MsgBox("维修人员尚未选择！，请选择！") : ComboBox4.Focus() : Exit Sub
        If a5 Then MsgBox("维修项止尚未选择！，请选择！") : ComboBox3.Focus() : Exit Sub

        Dim t6 As String = TextBox6.Text

        If TextBox6.Text = "" Then
            t6 = " -1"
        End If

        '写入数据库
        Dim sql As String = ""
        sql += "insert into 维修表(品牌,型号,维修单号,始修日期,项目,维修员,电话,地址,预测费用,备注)"
        sql += "values("
        sql += "'" + ComboBox1.Text + "',"
        sql += "'" + ComboBox2.Text + "',"
        sql += "'" + DH + "',"
        sql += "'" + Now.ToString + "',"
        sql += "'" + Trim(ComboBox3.Text) + "',"
        sql += "'" + Trim(ComboBox4.Text) + "',"
        sql += "'" + TextBox1.Text + "',"
        sql += "'" + TextBox2.Text + "',"
        sql += "'" + t6 + "',"
        sql += "'" + TextBox5.Text + "')"
        SQLupdata(sql)

        MsgBox("成功建立维修任务！")

        '初始化控件
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        ComboBox1.Items.Clear()
        ComboBox2.Items.Clear()
        ComboBox3.Items.Clear()
        ComboBox4.Items.Clear()

        '生成单号
        DH_calc()

        '加载品牌
        PP_Load()

        '加载维修项
        EV_Load()

        '加载维修员
        WP_Load()

        TextBox1.Focus()

    End Sub

    '选择品牌
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim sql As String = "select 型号 from 型号表 where 所属品牌 ='" + ComboBox1.Text + "'"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim RC As Integer = ds.Tables(0).Rows.Count
        If RC > 0 Then
            ComboBox2.Items.Clear()
            For i = 0 To RC - 1
                ComboBox2.Items.Add(ds.Tables(0).Rows(i).Item(0))
            Next
        End If
    End Sub

    '选择维修项
    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged

        Dim sql As String = "select 预测费用 from 维修项 where 项目 ='" + ComboBox3.Text + "'"
        Dim ds As DataSet = SQLtoDataSet(sql)
        Dim RC As Integer = ds.Tables(0).Rows.Count
        Debug.Print("模式恭恭敬敬：" + RC.ToString)
        YCFY = -1
        If RC > 0 Then
            If ds.Tables(0).Rows(0).IsNull(0) Then
                YCFY = -1
            Else
                YCFY = ds.Tables(0).Rows(0).Item(0)
            End If
        End If

    End Sub

    '显示费用
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            If YCFY >= 0 Then
                TextBox6.Text = YCFY.ToString
                TextBox6.Tag = TextBox6.Text
            Else
                TextBox6.Text = ""
                TextBox6.Tag = "-1"
            End If

        Else
            TextBox6.Text = ""
            TextBox6.Tag = ""
        End If

    End Sub


    Dim ZT_title As Font = New Font(New FontFamily("黑体"), 18)         '标题字体
    Dim ZT_Littl As Font = New Font(New FontFamily("宋体"), 12)         '页眉字体

    Dim ZT_phone As Font = New Font(New FontFamily("幼圆"), 10)         '页脚字体1
    Dim ZT_litt2 As Font = New Font(New FontFamily("幼圆"), 8)          '页脚字体2
    Dim ZT_titl2 As Font = New Font(New FontFamily("幼圆"), 15)         '标题字体2


    '打印按扭
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        '输入检测
        Dim a1 As Boolean = (Trim(TextBox1.Text) = "")
        Dim a6 As Boolean = (Trim(TextBox2.Text) = "")

        Dim a2 As Boolean = (Trim(ComboBox1.Text) = "")
        Dim a3 As Boolean = (Trim(ComboBox2.Text) = "")
        Dim a4 As Boolean = (Trim(ComboBox4.Text) = "")
        Dim a5 As Boolean = (Trim(ComboBox3.Text) = "")

        If a1 Then MsgBox("客户联系电话尚未填写！，请填写！") : TextBox1.Focus() : Exit Sub
        If a6 Then MsgBox("客户联系地址尚未填写！，请填写！") : TextBox2.Focus() : Exit Sub

        If a2 Then MsgBox("维修品牌尚未选择！，请选择！") : ComboBox1.Focus() : Exit Sub
        If a3 Then MsgBox("维修型号尚未选择！，请选择！") : ComboBox2.Focus() : Exit Sub
        If a4 Then MsgBox("维修人员尚未选择！，请选择！") : ComboBox4.Focus() : Exit Sub
        If a5 Then MsgBox("维修项止尚未选择！，请选择！") : ComboBox3.Focus() : Exit Sub

        CType(PrintPreviewDialog1, Form).WindowState = FormWindowState.Maximized
        PrintDocument1.DefaultPageSettings.PaperSize = New Printing.PaperSize(Printing.PaperKind.Custom, 815, 510)

        PrintPreviewDialog1.ShowDialog()

    End Sub
    '打印事件
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim g As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        Dim rect As Rectangle = e.MarginBounds

        '页眉
        g.DrawLine(Pens.Black, 40, 40, 810 - 50, 40)
        'g.DrawString("买得放心  用得开心", ZT_litt2, Brushes.Black, 40, 23)
        'g.DrawString("伟康电器  信心的保证", ZT_litt2, Brushes.Black, 693 - 50, 23)

        '标题：原设置常化设置，现改出这里的读写设置
        Dim TiS As TitleSet = ReadTx("REP.txt")
        g.DrawString(TiS.T1, ZT_title, Brushes.Black, TiS.T1x, TiS.T1y)
        g.DrawString(TiS.T2, ZT_title, Brushes.Black, TiS.T2x, TiS.T2y)
        g.DrawString(TiS.T3, ZT_title, Brushes.Black, TiS.T3x, TiS.T3y)

        '标题：原设置常化设置，现改出上面的读写设置
        'g.DrawString("东 莞 市 虎 门 伟 康 冷 冻 设 备", ZT_title, Brushes.Black, 213 - 50, 50)
        'g.DrawString("东 莞 市 虎 门 绍 康 电 器 商 店", ZT_title, Brushes.Black, 213 - 50, 95)
        'g.DrawString("商 品 销 售 单", ZT_title, Brushes.Black, 325 - 50 + 3, 135)

        '电话、电址
        g.DrawString("电话：85511208  85512003", ZT_phone, Brushes.Black, 220, 510 - 50)
        g.DrawString("地址：虎门执信路43号", ZT_phone, Brushes.Black, 40, 510 - 50)

        '打印日期、出单日期
        'g.DrawString("打印时间：" + Now.ToString, ZT_litt2, Brushes.Black, 650 - 50, 515 - 50)
        g.DrawString("日期：" + Now.ToString, ZT_phone, Brushes.Black, 640 - 50, 185)

        '客户、地址
        g.DrawString("客户：" + TextBox2.Text, ZT_phone, Brushes.Black, 42, 160)
        g.DrawString("联系：" + TextBox1.Text, ZT_phone, Brushes.Black, 42, 185)

        '单号：
        g.DrawString(Label9.Text, ZT_titl2, Brushes.Black, 640 - 50, 157)

        '表格
        DrawTable(g, pen, rect)

        '表头
        g.DrawString("品 名 规 格", ZT_phone, Brushes.Black, 100 - 25, 220)
        g.DrawString("修 讫 核 证 信 息", ZT_phone, Brushes.Black, 100 - 25, 333)

        g.DrawString("维修人员", ZT_phone, Brushes.Black, 340 - 60, 220)
        g.DrawString("维  修  项  目", ZT_phone, Brushes.Black, 467 - 30, 220)
        g.DrawString("预测费用", ZT_phone, Brushes.Black, 721 - 55, 220)
     

        '填表
        DrawList(g, pen, rect)
    End Sub
    '画表
    Private Sub DrawTable(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)

        '竖边
        g.DrawLine(Pens.Black, 41, 217, 41, 322)
        g.DrawLine(Pens.Black, 41, 330, 41, 496 - 60)

        g.DrawLine(Pens.Black, 809 - 50, 217, 809 - 50, 322)
        g.DrawLine(Pens.Black, 809 - 50, 330, 809 - 50, 496 - 60)

        '横边
        g.DrawLine(Pens.Black, 40, 213, 810 - 50, 213)
        g.DrawLine(Pens.Black, 40, 500 - 60, 810 - 50, 500 - 60)


        '内横
        For i = 1 To 2
            g.DrawLine(Pens.Black, 48, 211 + (30 * i), 802 - 50, 211 + (30 * i))
        Next

        '中横
        g.DrawLine(Pens.Black, 40, 211 + (30 * 3) + 25, 810 - 50, 211 + (30 * 3) + 25)

        g.DrawLine(Pens.Black, 48, 211 + (30 * 4) + 25, 802 - 50, 211 + (30 * 4) + 25)

        '内竖--中线
        g.DrawLine(Pens.Black, 425 - 50, 217, 425 - 50, 267)

        '内竖--后三分显一
        g.DrawLine(Pens.Black, 681 - 50, 217, 681 - 50, 267)

        '内竖--前三份显一
        g.DrawLine(Pens.Black, 296 - 50, 217, 296 - 50, 267)
        g.DrawLine(Pens.Black, 296 - 50, 330, 296 - 50, 352)

    End Sub
    '填表
    Private Sub DrawList(ByVal g As Graphics, ByVal pen As Pen, ByVal rect As Rectangle)
        Dim y As Integer = 248

        '品牌型号
        g.DrawString(ComboBox1.Text + " " + ComboBox2.Text, ZT_phone, Brushes.Black, 75, y)
        '维修人员
        g.DrawString(ComboBox4.Text, ZT_phone, Brushes.Black, 280, y)
        '维修项目
        g.DrawString(ComboBox3.Text, ZT_phone, Brushes.Black, 437, y)

        '预测费用
        If CheckBox1.Checked And TextBox6.Text <> "" Then
            g.DrawString("￥" + TextBox6.Text + "元", ZT_phone, Brushes.Black, 666, y)
        Else
            g.DrawString("未确定", ZT_phone, Brushes.Black, 666, y)
        End If

        If Trim(TextBox5.Text) <> "" Then
            g.DrawString("备注：" + TextBox5.Text, ZT_phone, Brushes.Black, 75, 289)
        End If

    End Sub

End Class